package com.example.basic.web.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.basic.core.foreignData.ForeignDataInParam;
import com.example.basic.core.foreignData.ForeignDataResult;
import com.example.basic.core.foreignData.ForeignDataResultGenerator;
import com.example.basic.core.foreignData.HoistDataInParam;
import com.example.basic.core.result.Result;
import com.example.basic.core.result.ResultGenerator;
import com.example.basic.custom.Log;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.StringUtils;
import com.example.basic.web.model.terminal.TerminalRecord;
import com.example.basic.web.service.common.CommonService;
import com.example.basic.web.service.terminal.TerminalRecordService;
import com.example.basic.web.service.transTask.TransTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.example.basic.utils.CheckUtil.notNull;

@Api(value = "公共接口相关操作", tags = {"公共接口"})
@RestController
@RequestMapping("/api/v1/common")
public class CommonController {

    @Resource
    private CommonService commonService;

    @Resource
    private TransTaskService transTaskService;

    @Value("${spring.foreigndata.authorization}")
    private String autorization;

    /**
     * @create 获取数据库当前时间 zhangzl
     * @param request
     * @return
     */
    @Log(title = "公共接口", description = "获取数据库当前时间")
    @ApiOperation(value="获取数据库当前时间",notes="获取数据库当前时间")
    @PostMapping("/getDataBaseTime")
    public Date getDataBaseTime(
            HttpServletRequest request){
        return commonService.getDataBaseTime();
    }

    /**
     * 调度任务完成
     * @param foreignDataInParam
     * @return
     * @throws IOException
     */
    @PostMapping("/dataTransfor")
    public Result dataTransfor(@RequestBody ForeignDataInParam foreignDataInParam) throws IOException {
        System.out.printf("收到回调消息，消息为"+foreignDataInParam);
        notNull(foreignDataInParam,"参数不能为空");

        String failInfo = "";

        if(StringUtils.emptyParams(foreignDataInParam.getAuthorization())){
            failInfo = "令牌不能为空";
        }

        if(!"".equals(failInfo)){
            throw new ServiceException(failInfo);
        }

        if(!autorization.equals(foreignDataInParam.getAuthorization())){
            throw new ServiceException("令牌有误");
        }

        return ResultGenerator.genSuccessResult(transTaskService.singleTaskFinish(foreignDataInParam));
    }

    /**
     * 更新提升机设备为空闲
     * @param hoistDataInParam
     * @return
     * @throws IOException
     */
    @PostMapping("/modifyHoistStatus")
    public Result modifyHoistStatus(@RequestBody HoistDataInParam hoistDataInParam) throws IOException {
        notNull(hoistDataInParam,"参数不能为空");

        String failInfo = "";

        if(StringUtils.emptyParams(hoistDataInParam.getAuthorization())){
            failInfo = "令牌不能为空";
        }

        if(StringUtils.emptyParams(hoistDataInParam.getDeviceId())){
            failInfo = "设备不能为空";
        }

        if(!"".equals(failInfo)){
            throw new ServiceException(failInfo);
        }

        if(!autorization.equals(hoistDataInParam.getAuthorization())){
            throw new ServiceException("令牌有误");
        }

        return ResultGenerator.genSuccessResult(transTaskService.modifyHoistStatus(hoistDataInParam));
    }
}
